#!/bin/bash
lovaid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups?search=LoVa-service"|jq .[].id`
lova=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups/$lovaid/projects"?per_page=300|jq .[].name`
csid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups?search=comm-service"|jq .[].id`
cs=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups/$csid/projects"?per_page=300|jq .[].name`
micid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups?search=micro-app"|jq .[].id`
mic=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups/$csid/projects"?per_page=300|jq .[].name`
nfseid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups?search=FSE"|jq .[].id`
nfse=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups/$nfseid/projects"?per_page=300|jq .[].name`

for i in `kubectl get pod -A|grep -v "kube-system\|rook-ceph\|public-service\|monitoring\|logging\|istio-system\|cattle-system\|cattle-fleet-system\|default"|grep 1/2|awk '{print $1"_"$2"_"$5}'`
do
  ns=`echo $i|awk -F '_' '{print $1}'`
  podname=`echo $i|awk -F '_' '{print $2}'`
  num=`echo $i|awk -F '_' '{print $3}'`
  deploymentname=`echo $podname|awk -F"-" '{ for (i = 1; i <= NF-2; i++) { printf "%s", $i; if (i < NF-2) printf "-" } printf "\n" }'`
  flag=`kubectl describe deployment $deploymentname -n $ns|grep Progressing|awk '{print $2}'`
  if [[ `echo $num` -gt 1 || $flag == "False" ]];then
     echo "restart $deploymentname with $ns"
     kubectl scale deployment $deploymentname --replicas=0 -n $ns && \
     kubectl scale deployment $deploymentname --replicas=1 -n $ns
     countzz=`cat /tmp/restartpod`
     count=$((countzz+1))
     echo $count > /tmp/restartpod
     if [[ $count -gt 1 ]];then
      kubectl scale deployment $deploymentname --replicas=0 -n $ns
      echo 0 > /tmp/restartpod
      ver=`kubectl describe deployment $deploymentname -n $ns|grep Image|awk -F : '{print $NF}'`
      echo "ver="$ver
      ######################################
      #project_id=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" http://10.202.16.73:8900/api/v4/projects?search=$deploymentname|jq '.[] | select(.name == "${deploymentname}") | .id'`
      PAGE=1
      PER_PAGE=100
      while true; do
        RESPONSE=$(curl --silent --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/projects?per_page=$PER_PAGE&page=$PAGE")
        if [ "$(echo $RESPONSE | jq '. | length')" -eq 0 ]; then
           break  
        fi

        if [[ " ${lova[@]} " =~ $deploymentname ]];then
           if [[ ! -z `echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "LoVa-service" and .name == $deploymentname)| .id'` ]];then
              echo "This is lova"
              project_id=`echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "LoVa-service" and .name == $deploymentname)| .id'`
              #echo "project_id="$project_id
           fi
        elif [[ " ${cs[@]} " =~ $deploymentname ]];then
           if [[ ! -z `echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "comm-service" and .name == $deploymentname)| .id'` ]];then
              echo "This is cs"
              project_id=`echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "comm-service" and .name == $deploymentname)| .id'`
              #echo "project_id="$project_id
           fi
        elif [[ " ${mic[@]} " =~ $deploymentname ]];then
           if [[ ! -z `echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "micro-app" and .name == $deploymentname)| .id'` ]];then
              echo "This is micro-app"
              project_id=`echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "micro-app" and .name == $deploymentname)| .id'`
              #echo "project_id="$project_id
           fi
        elif [[ " ${nfse[@]} " =~ $deploymentname ]];then
           if [[ ! -z `echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "FSE" and .name == $deploymentname)| .id'` ]];then
              echo "This is nfse"
              project_id=`echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "FSE" and .name == $deploymentname)| .id'`
              #echo "project_id="$project_id
           fi
        fi
        PAGE=$((PAGE + 1))
      done
      ###################################################
      echo "project_id="$project_id
      commitid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/projects/$project_id/repository/tags"|jq '.[] | select(.name |contains ("${ver}")) | .commit.short_id'`
      echo "commitid="$commitid
      if [[ -z $commitid ]];then
        piplineid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/projects/$project_id/pipelines"|jq '.[].id'|head -n 1`
      else
        piplineid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/projects/$project_id/pipelines"|jq --arg commitid "$commitid" '.[] | select(.sha |contains ($commitid)) | .id'`
      fi
      echo "piplineid="$piplineid
      userid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/projects/$project_id/pipelines/$piplineid"|jq '.user.id'`
      echo "userid="$userid
      emailzz=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/users/$userid"|jq '.email'|awk -F'"' '{print $2}'`
      echo "emailzz="$emailzz
      ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "The ${podname} service at ${ns} environment Service abnormal!"|mail -s Microservice\ relese\ exception  zhang.zhe@y3.sg,$emailzz"
     fi
  fi
done

